package com.crb.crm.manage.service.biz.impl;

import com.crb.crm.manage.utils.DateUtil;
import com.crb.crm.manage.dao.biz.MessageSendRecordInfoMapper;
import com.crb.crm.manage.dao.biz.MessageSendRecordMapper;
import com.crb.crm.manage.entity.biz.MessageSendRecordDTO;
import com.crb.crm.manage.entity.biz.MessageSendRecordSearchDTO;
import com.crb.crm.manage.entity.biz.MessageSendRecordVO;
import com.crb.crm.manage.pojo.biz.MessageSendRecord;
import com.crb.crm.manage.service.biz.MessageSendRecordService;
import com.crb.crm.manage.utils.PageDataResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageSendRecordServiceImpl implements MessageSendRecordService {
	private static final Logger logger = LoggerFactory.getLogger(MessageSendRecordServiceImpl.class);

	@Autowired
	private MessageSendRecordInfoMapper messageSendRecordInfoMapper;
	@Autowired
	private MessageSendRecordMapper messageSendRecordMapper;

	@Override
	public PageDataResult getMessageSendRecords(MessageSendRecordSearchDTO messageSendRecordSearch, int page, int limit) {
		// 时间处理
		if (null != messageSendRecordSearch) {
			if (StringUtils.isNotEmpty(messageSendRecordSearch.getQinsertTimeStart())
					&& StringUtils.isEmpty(messageSendRecordSearch.getQinsertTimeEnd())) {
				messageSendRecordSearch.setQinsertTimeEnd(DateUtil.format(new Date()));
			} else if (StringUtils.isEmpty(messageSendRecordSearch.getQinsertTimeStart())
					&& StringUtils.isNotEmpty(messageSendRecordSearch.getQinsertTimeEnd())) {
				messageSendRecordSearch.setQinsertTimeStart(DateUtil.format(new Date()));
			}
			if (StringUtils.isNotEmpty(messageSendRecordSearch.getQinsertTimeStart())
					&& StringUtils.isNotEmpty(messageSendRecordSearch.getQinsertTimeEnd())) {
				if (messageSendRecordSearch.getQinsertTimeEnd().compareTo(messageSendRecordSearch.getQinsertTimeStart()) < 0) {
					String temp = messageSendRecordSearch.getQinsertTimeStart();
					messageSendRecordSearch.setQinsertTimeStart(messageSendRecordSearch.getQinsertTimeEnd());
					messageSendRecordSearch.setQinsertTimeEnd(temp);
				}
			}
		}
		PageDataResult pdr = new PageDataResult();
		PageHelper.startPage(page, limit);
		List<MessageSendRecordDTO> urList = messageSendRecordInfoMapper.getMessageSendRecord(messageSendRecordSearch);
		// 获取分页查询后的数据
		PageInfo<MessageSendRecordDTO> pageInfo = new PageInfo<>(urList);
		// 设置获取到的总记录数total：
		pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
		pdr.setList(urList);
		return pdr;
	}

	@Override
	public MessageSendRecordVO getMessageSendRecord(Integer id) {
		MessageSendRecord messageSendRecord = messageSendRecordMapper.selectByPrimaryKey(id);
		if (messageSendRecord != null) {
			MessageSendRecordVO vo = new MessageSendRecordVO();
			BeanUtils.copyProperties(messageSendRecord, vo);
			return vo;
		}
		return null;
	}


}
